﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Model;
using AvatarHelper;

public partial class Management_LinkManager : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            panel_Edit.Visible = false;
        }
    }
    protected void btn_Save_Click(object sender, EventArgs e)
    {
        try
        {
            #region 入口检测
            if (string.IsNullOrEmpty(txt_SiteName.Text)) throw new Exception("网站名称不能为空");
            if (string.IsNullOrEmpty(txt_URL.Text)) throw new Exception("网站地址不能为空");

            if (!string.IsNullOrEmpty(txt_SiteName.Text) && txt_SiteName.Text.Length > 20) throw new Exception("网站名称不能大于20个字符");
            if (!string.IsNullOrEmpty(txt_URL.Text) && txt_URL.Text.Length > 200) throw new Exception("网站网址不能大于200个字符");
            #endregion

            using (var _adc = DBHelper.GetDataContext())
            {
                var _id = Convert.ToInt32(txt_ID.Value);

                var _link = _adc.FriendLink.FirstOrDefault(l => l.ID == _id);
                if (_link != null)
                {
                    _link.ID = Convert.ToInt32(txt_ID.Value);
                    try
                    {
                        _link.Order = Convert.ToInt32(txt_Order.Text);
                    }
                    catch { throw new Exception("排序序号填写错误"); }

                    _link.SiteName = txt_SiteName.Text;
                    _link.URL = txt_URL.Text;
                    _link.Open = rbl_Close.SelectedIndex == 0 ? false : true;

                    _adc.SubmitChanges();
                }
                txt_ID.Value = "";
                txt_Order.Text = "";
                txt_SiteName.Text = "";
                txt_URL.Text = "";
                rbl_Close.ClearSelection();
                panel_Edit.Visible = false;

                GridView1.DataBind();
            }
        }
        catch(Exception _e)
        {
            lbl_Error.Text = "错误:" + _e.Message;
        }
    }
    protected void btn_Select_Click(object sender, EventArgs e)
    {
        panel_Edit.Visible = true;
        var _btn = sender as Button;
        var _id = Convert.ToInt32(_btn.CommandArgument);
        using (var _adc = DBHelper.GetDataContext())
        {
            var _link = _adc.FriendLink.FirstOrDefault(l => l.ID == _id);
            if (_link != null)
            {
                txt_ID.Value = _link.ID.ToString();
                txt_Order.Text = _link.Order.ToString();
                txt_SiteName.Text = _link.SiteName;
                txt_URL.Text = _link.URL;
                rbl_Close.SelectedIndex = _link.Open == true ? 1 : 0;
            }
            else
            {
                GridView1.DataBind();
            }
        }
    }
    protected void btn_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect(Request.Url.ToString());
    }
    protected void btn_Del_Click(object sender, EventArgs e)
    {
        var _btn = sender as Button;
        var _id = Convert.ToInt32(_btn.CommandArgument);
        using (var _adc = DBHelper.GetDataContext())
        {
            var _link = _adc.FriendLink.FirstOrDefault(l => l.ID == _id);
            if (_link != null)
            {
                _adc.FriendLink.DeleteOnSubmit(_link);
                _adc.SubmitChanges();
                GridView1.DataBind();
            }
            else
            {
                GridView1.DataBind();
            }
        }
    }
}
